﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Data.SQLite;
using System.Text;

public partial class joke_list : BaseShowPage
{
    private int pageSize = 10;
    protected string JokeListHtml = "";
    protected string PageHtml = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitJokeList();
        }
    }
    private void InitJokeList()
    {
        string typeParam = Request["type"];
        int type = 50;
        if(typeParam != null)
        {
            type = ParamHelper.ParseIntPositive(typeParam);
        }
        int page = ParamHelper.ParseInt(Request["page"], 1);        
        DataRow row = null;
        string typeCondition = "";
        if (type > 0 && type != 50)
        {
            row = PageData.SelectParamRow("typeCode='joke" + type.ToString() + "'");
            if (row == null)
            {
                row = PageData.SelectParamRow("typeCode='joke'");
            }
            else
            {
                typeCondition = " where type=" + type.ToString();
            }
        }
        else
        {
            row = PageData.SelectParamRow("typeCode='joke'");
        }        

        Master.SetTitleKeyDesc(row);

        int total = int.Parse(row[PageColumn.totalCount].ToString());
        int start = PageData.CalculateStart(page.ToString(), total, pageSize);
        PageHtml = PageData.GetPageHtml(total, pageSize, page, "list.aspx?type=" + type + "&page=pageN");

        string sql = "select id, title, content from JokePublish " + typeCondition + " order by id desc limit " + start.ToString() + "," + pageSize.ToString();
        StringBuilder sbHtml = new StringBuilder(8192);
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader reader = null;

        try
        {
            bool isFirst = true;
            conn.Open();                
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (isFirst)
                {
                    isFirst = false;                   
                    sbHtml.Append("<dl>");
                }
                sbHtml.Append("<dt><a href=\"content.aspx?id=").Append(reader["id"].ToString()).Append("\" target=\"_blank\">");
                sbHtml.Append(reader["title"].ToString()).Append("</a></dt>");

                sbHtml.Append("<dd>").Append(reader["content"].ToString()).Append("</dd>");
            }            
            if (!isFirst)//isFirst = false;说明已经读取到数据，加上结束标签
            {
                sbHtml.Append("</dl>");
            }           
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
        JokeListHtml = sbHtml.ToString();
    }
}
